package com.edge.pbx.view.eq;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;



public class IndicatorView extends View implements ViewPager.OnPageChangeListener, ViewPager.OnAdapterChangeListener {
    private int mWidth, mHeight;
    private int mIndicatorCount = 4;
    private int mIndex;
    private int mCircleRadius = 1;
    private int mCircleSpace = 1;
    private int mFirstCircleX = 1;
    private int mCenterY;
    private Paint mPaint;
    private int [] mColors;
    public IndicatorView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mColors = new int[]{
//                0xFF00a199, 0xFF0dac67, 0xFF2ca6e0, 0xFFf7b52c
                0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF
        };
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        final int width = right - left;
        final int heigh = bottom - top;
        if (mWidth != width || mHeight != heigh){
            mWidth = width;
            mHeight = heigh;
            mCenterY = heigh>>1;
            cal(false);
        }
    }

    private void cal(boolean refresh){
        // 1  2  3  4
        //1001001001001001
        //直径径
//        int diameter = (int) Math.min(mHeight*0.6f, mWidth/(mIndicatorCount*3 -2 ));
        int diameter = (int) (mHeight * 0.4f);
        mCircleSpace = diameter*2;
        while (( diameter*mIndicatorCount + mCircleSpace*(mIndicatorCount+1) ) > mWidth){
            diameter--;
            mCircleSpace = diameter*2;
        }

        mCircleRadius = diameter >> 1;
        mFirstCircleX = ( mWidth - diameter*mIndicatorCount - mCircleSpace*(mIndicatorCount-1))/2 + mCircleSpace/2;
        Log.d("Yang"," cal w:"+ mWidth +" h:"+ mHeight +" r:"+ mCircleRadius +" space:"+ mCircleSpace);
        if (refresh )postInvalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float tmp ;
        if (mScrollState == ViewPager.SCROLL_STATE_IDLE){
            for (int i = 0; i < mIndicatorCount; i++) {

                if (i == mIndex){
                    mPaint.setColor(0xFFed6b1b);
                    canvas.drawCircle(mFirstCircleX + i*(mCircleRadius+mCircleSpace), mCenterY, mCircleRadius*1.3f,mPaint);
                }else {
                    mPaint.setColor(mColors[i]);
                    canvas.drawCircle(mFirstCircleX + i*(mCircleRadius+mCircleSpace), mCenterY, mCircleRadius,mPaint);
                }
            }
        }else {
            for (int i = 0; i < mIndicatorCount; i++) {
                if (i == mIndex) {
                    mPaint.setColor(0xFFed6b1b);
                }else{
                    mPaint.setColor(mColors[i]);
                }

                if (i == mScrollPosition){
                    tmp = (1f - mScrollOffset) *0.3f +1f;
                }else if (i == mScrollPosition+1){
                    tmp = 1f + mScrollOffset*0.3f;
                }else {
                    tmp = 1f;
                }
                canvas.drawCircle(mFirstCircleX + i*(mCircleRadius+mCircleSpace), mCenterY, mCircleRadius*tmp,mPaint);
            }
        }

    }

    public void setViewPager(ViewPager viewPager){
        viewPager.addOnPageChangeListener(this);
        viewPager.addOnAdapterChangeListener(this);
    }
    public void setIndicatorCount(int indicatorCount) {
        if (mIndicatorCount == indicatorCount)return;
        mIndicatorCount = indicatorCount;
        cal(true);
    }

    public int getIndicatorCount() {
        return mIndicatorCount;
    }

    public int getIndex() {
        return mIndex;
    }

    public void setIndex(int index) {
        setIndex(index, false);
    }
    public void setIndex(int index,boolean anim) {
        mIndex = index;
    }

    private int mScrollState;
    private int mScrollPosition = 0;
    private float mScrollOffset = 0f;
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        mScrollPosition = position;
        mScrollOffset = positionOffset;
        postInvalidate();
        Log.d("Yang", "onPageScrolled:"+ position + " " + positionOffset +" " + positionOffsetPixels);
    }

    @Override
    public void onPageSelected(int position) {
        Log.d("Yang","onPageSelected:"+position);
        setIndex(position,false);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        Log.d("Yang","onPageScrollStateChanged:"+state);
        mScrollState = state;
    }

    @Override
    public void onAdapterChanged(@NonNull ViewPager viewPager, @Nullable PagerAdapter oldAdapter, @Nullable PagerAdapter newAdapter) {
        setIndicatorCount(newAdapter.getCount());
    }
}

